using System;
using System.Collections;
using System.Data;
using BeeldBank.Business;
using BeeldBank.Business.Collection;

namespace BeeldBank.Business.Base
{
	/// <summary>
	/// Provides database independant methods for User.
	/// </summary>
	public class UserBase : BusinessBase
	{
		internal UserBase(System.Data.DataRow row) : base(row)
		{
			this.OnPrepareForSave += new PrepareForSaveHandler(this.PrepareForSave);
		}

		public delegate void PrepareForSaveHandler();
		public event PrepareForSaveHandler OnPrepareForSave;

		#region property bool EnforceSave
		/// This property is used to indicate if the
		/// object in the save event should save, even
		/// if the object contents (marked as stereotype NOTENFORCESAVE) is not saved
		private bool _EnforceSave=false;
		public bool EnforceSave
		{
			get
			{
				return _EnforceSave;
			}
			set
			{
				_EnforceSave = value;
			}
		
		}
		#endregion



		#region PrepareForSaveBase()
		public void PrepareForSaveBase()
		{
			if (!this.PrepareForSaveFinished)
			{
				if (OnPrepareForSave != null)
				{
					OnPrepareForSave();
				}
				this.CanLoad = false;
				// call prepareforsave on all dependant objects.
			
				this.CanLoad = true;
				this.PrepareForSaveFinished = true;
			}
		}
		#endregion

		#region PrepareForSave()
		public virtual void PrepareForSave()
		{
		}
		#endregion

		#region PreSave()
		public virtual void PreSave()
		{
		}
		#endregion
				
		#region Save()
		public void Save()
		{
			bool IsSaveNeeded=ContentChanged();
			
			if (IsDirty)
			{
				this.ModifiedBy = _luw.UserName;
				if (this.State == BusinessState.New)
				{
					this.CreatedBy = _luw.UserName;
					BeeldBank.Data.User.Insert(this._dataRow, _luw.Transaction);
					
				}
			
				if ( ( this.State == BusinessState.Changed ) && ( IsSaveNeeded ) )
				{
					BeeldBank.Data.User.Update(this._dataRow, _luw.Transaction);
				}
				
				if ( this.State == BusinessState.Deleted )
				{
					BeeldBank.Data.User.Delete(this._dataRow, _luw.Transaction);
					
				}
				IsDirty = false;
			}
		}
		#endregion
		
		#region OldUser
		private User _oldUser;
		public User OldUser
		{
			get
			{
				if (_oldUser == null && this.State != BusinessState.New)
				{
					// Create the new object in a completely separate luw,
					// otherwise a reference the the current object is returned.
					Luw luw = new Luw(this.Luw.UserName);
					_oldUser = luw.NewUser(BeeldBank.Data.User.Open(this.Id).Rows[0]);
				}
				return _oldUser;
			}
		}
		#endregion
		
		#region Id
		/// <summary>
		/// 
		/// </summary>
		public int Id
		{
			get
			{
				return Convert.ToInt32(_dataRow["Id"]);
			}
			set
			{
				_dataRow["Id"] = value;
				this.IsDirty = true;
			}
		}
		public bool IdIsNull
		{
			get
			{
				return _dataRow["Id"] == Convert.DBNull;
			}
		}
		public void IdSetNull()
		{
			_dataRow["Id"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Username
		/// <summary>
		/// Gebruikersnaam
		/// </summary>
		public string Username
		{
			get
			{
				return Convert.ToString(_dataRow["Username"]);
			}
			set
			{
				_dataRow["Username"] = value;
				this.IsDirty = true;
			}
		}
		public bool UsernameIsNull
		{
			get
			{
				return _dataRow["Username"] == Convert.DBNull;
			}
		}
		public void UsernameSetNull()
		{
			_dataRow["Username"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region FirstName
		/// <summary>
		/// Voornaam
		/// </summary>
		public string FirstName
		{
			get
			{
				return (_dataRow["FirstName"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["FirstName"]);
			}
			set
			{
				_dataRow["FirstName"] = value;
				this.IsDirty = true;
			}
		}
		public bool FirstNameIsNull
		{
			get
			{
				return _dataRow["FirstName"] == Convert.DBNull;
			}
		}
		public void FirstNameSetNull()
		{
			_dataRow["FirstName"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region LastName
		/// <summary>
		/// Achternaam
		/// </summary>
		public string LastName
		{
			get
			{
				return (_dataRow["LastName"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["LastName"]);
			}
			set
			{
				_dataRow["LastName"] = value;
				this.IsDirty = true;
			}
		}
		public bool LastNameIsNull
		{
			get
			{
				return _dataRow["LastName"] == Convert.DBNull;
			}
		}
		public void LastNameSetNull()
		{
			_dataRow["LastName"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region IsSuperUser
		/// <summary>
		/// Is de gebruiker een superuser
		/// </summary>
		public bool IsSuperUser
		{
			get
			{
				return (_dataRow["IsSuperUser"] == Convert.DBNull) ? false : Convert.ToBoolean(_dataRow["IsSuperUser"]);
			}
			set
			{
				_dataRow["IsSuperUser"] = value;
				this.IsDirty = true;
			}
		}
		public bool IsSuperUserIsNull
		{
			get
			{
				return _dataRow["IsSuperUser"] == Convert.DBNull;
			}
		}
		public void IsSuperUserSetNull()
		{
			_dataRow["IsSuperUser"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region AffiliateId
		/// <summary>
		/// Affiliate
		/// </summary>
		public int AffiliateId
		{
			get
			{
				return (_dataRow["AffiliateId"] == Convert.DBNull) ? 0 : Convert.ToInt32(_dataRow["AffiliateId"]);
			}
			set
			{
				_dataRow["AffiliateId"] = value;
				this.IsDirty = true;
			}
		}
		public bool AffiliateIdIsNull
		{
			get
			{
				return _dataRow["AffiliateId"] == Convert.DBNull;
			}
		}
		public void AffiliateIdSetNull()
		{
			_dataRow["AffiliateId"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Email
		/// <summary>
		/// Email adres van de gebruiker
		/// </summary>
		public string Email
		{
			get
			{
				return (_dataRow["Email"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Email"]);
			}
			set
			{
				_dataRow["Email"] = value;
				this.IsDirty = true;
			}
		}
		public bool EmailIsNull
		{
			get
			{
				return _dataRow["Email"] == Convert.DBNull;
			}
		}
		public void EmailSetNull()
		{
			_dataRow["Email"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region DisplayName
		/// <summary>
		/// De naam die getoond wordt in de applicatie
		/// </summary>
		public string DisplayName
		{
			get
			{
				return (_dataRow["DisplayName"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["DisplayName"]);
			}
			set
			{
				_dataRow["DisplayName"] = value;
				this.IsDirty = true;
			}
		}
		public bool DisplayNameIsNull
		{
			get
			{
				return _dataRow["DisplayName"] == Convert.DBNull;
			}
		}
		public void DisplayNameSetNull()
		{
			_dataRow["DisplayName"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region UpdatePassword
		/// <summary>
		/// Dit veld geeft aan of de gebruiker zijn password moet wijzigen bij de volgende keer aanloggen
		/// </summary>
		public bool UpdatePassword
		{
			get
			{
				return (_dataRow["UpdatePassword"] == Convert.DBNull) ? false : Convert.ToBoolean(_dataRow["UpdatePassword"]);
			}
			set
			{
				_dataRow["UpdatePassword"] = value;
				this.IsDirty = true;
			}
		}
		public bool UpdatePasswordIsNull
		{
			get
			{
				return _dataRow["UpdatePassword"] == Convert.DBNull;
			}
		}
		public void UpdatePasswordSetNull()
		{
			_dataRow["UpdatePassword"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region CreatedBy
		public String CreatedBy
		{
			get
			{
				return Convert.ToString(_dataRow["CreatedBy"]);
			}
			set
			{
				_dataRow["CreatedBy"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region CreatedAt
		public DateTime CreatedAt
		{
			get
			{
				return Convert.ToDateTime(_dataRow["CreatedAt"]);
			}
			set
			{
				_dataRow["CreatedAt"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ModifiedBy
		public String ModifiedBy
		{
			get
			{
				return Convert.ToString(_dataRow["ModifiedBy"]);
			}
			set
			{
				_dataRow["ModifiedBy"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ModifiedAt
		public DateTime ModifiedAt
		{
			get
			{
				return Convert.ToDateTime(_dataRow["ModifiedAt"]);
			}
			set
			{
				_dataRow["ModifiedAt"] = value;
				this.IsDirty = true;
			}
		}
		#endregion
	
		#region Version
		public int Version
		{
			get
			{
				return _dataRow["Version"] == Convert.DBNull ? -1 : Convert.ToInt32(_dataRow["Version"]);
			}
			set
			{
				_dataRow["Version"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region Orders
		private OrderCollection _orders;
		public OrderCollection Orders
		{
			get
			{
				if (_orders == null && _luw.CanLoad && this.CanLoad && !IdIsNull)
				{
					_orders = new OrderCollection(_luw, BeeldBank.Data.Order.OpenFromUserId(Id));
				}
				if (_orders == null && _luw.CanLoad && this.CanLoad && IdIsNull)
				{
					_orders = new OrderCollection(_luw);
				}
				return _orders;
			}				
		}
		#endregion
		
		#region ZoekActie
		private ZoekActieLogCollection _zoekActie;
		public ZoekActieLogCollection ZoekActie
		{
			get
			{
				if (_zoekActie == null && _luw.CanLoad && this.CanLoad && !IdIsNull)
				{
					_zoekActie = new ZoekActieLogCollection(_luw, BeeldBank.Data.ZoekActieLog.OpenFromUserId(Id));
				}
				if (_zoekActie == null && _luw.CanLoad && this.CanLoad && IdIsNull)
				{
					_zoekActie = new ZoekActieLogCollection(_luw);
				}
				return _zoekActie;
			}				
		}
		#endregion
		

		#region ContentChanged
		/// <summary>
		/// ContentChanged
		/// This function checks whether the content of a record is changed, dependant on the "EnforceSave" 
		/// variable. If EnforceSave is false, all the fields which have not been marked "NOTENFORCINGSAVE" as
		/// a stereotype in the design are checked. The contents of the fields are checked against the old values
		/// if they are not equal,the result is true. If the contents are equal, the result is false.
		/// 
		/// If EnforceSave is true, the result is always true.
		/// </summary>
		/// <returns>boolean indicating whether the Content is Changed</returns>
		public bool ContentChanged()
		{
			// Change enforced by Class Variables
			if( this.EnforceSave ) { return( true ); }
			if( this.IdIsNull) { return( true ); }
			
			// Change enforced by the attributes
			
			#region Id
			/// Id is excluded because it is the primary key
			#endregion
					
			#region Username
			
			if( this.UsernameIsNull != this.OldUser.UsernameIsNull ) { return( true ); }
			if( this.Username != this.OldUser.Username) { return( true ); }
	
			#endregion
					
			#region FirstName
			
			if( this.FirstNameIsNull != this.OldUser.FirstNameIsNull ) { return( true ); }
			if( this.FirstName != this.OldUser.FirstName) { return( true ); }
	
			#endregion
					
			#region LastName
			
			if( this.LastNameIsNull != this.OldUser.LastNameIsNull ) { return( true ); }
			if( this.LastName != this.OldUser.LastName) { return( true ); }
	
			#endregion
					
			#region IsSuperUser
			
			if( this.IsSuperUserIsNull != this.OldUser.IsSuperUserIsNull ) { return( true ); }
			if( this.IsSuperUser != this.OldUser.IsSuperUser) { return( true ); }
	
			#endregion
					
			#region AffiliateId
			
			if( this.AffiliateIdIsNull != this.OldUser.AffiliateIdIsNull ) { return( true ); }
			if( this.AffiliateId != this.OldUser.AffiliateId) { return( true ); }
	
			#endregion
					
			#region Email
			
			if( this.EmailIsNull != this.OldUser.EmailIsNull ) { return( true ); }
			if( this.Email != this.OldUser.Email) { return( true ); }
	
			#endregion
					
			#region DisplayName
			
			if( this.DisplayNameIsNull != this.OldUser.DisplayNameIsNull ) { return( true ); }
			if( this.DisplayName != this.OldUser.DisplayName) { return( true ); }
	
			#endregion
					
			#region UpdatePassword
			
			if( this.UpdatePasswordIsNull != this.OldUser.UpdatePasswordIsNull ) { return( true ); }
			if( this.UpdatePassword != this.OldUser.UpdatePassword) { return( true ); }
	
			#endregion
							
			
			return false;
		}
		#endregion	

		
		#region WhereAmI
		/// <summary>
		/// Provides the WhereAmI display text for a User with the specified id
		/// </summary>
		/// <param name="id">Id of the User.</param>
		/// <return>>WhereAmI display text</returns>
		public static string WhereAmI(int id)
		{
			string whereAmI = "";
			if (id == 0)
			{
				whereAmI = "New User";
			}
			else
			{
				DataTable table = BeeldBank.Data.User.Show(id);
				
				whereAmI = whereAmI + table.Rows[0]["Username"].ToString();
				
			}
			return whereAmI;
		}
		#endregion
		
	}
	

	
	
}
